package com.ruoyi.flowable.config;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.flowable.ui.common.repository.UuidIdGenerator;
import org.flowable.ui.common.tenant.TenantProvider;
import org.flowable.ui.modeler.domain.ModelHistory;
import org.flowable.ui.modeler.repository.ModelHistoryRepository;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ModelHistoryRepositoryImpl implements ModelHistoryRepository {
    private static final String NAMESPACE = "org.flowable.ui.modeler.domain.ModelHistory.";
    @Autowired
    protected SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    protected UuidIdGenerator idGenerator;
    @Autowired
    protected TenantProvider tenantProvider;

    public ModelHistoryRepositoryImpl() {
    }

    public ModelHistory get(String id) {
        return (ModelHistory)this.sqlSessionTemplate.selectOne("org.flowable.ui.modeler.domain.ModelHistory.selectModelHistory", id);
    }

    public List<ModelHistory> findByModelTypAndCreatedBy(String createdBy, Integer modelType) {
        Map<String, Object> params = new HashMap();
        params.put("modelType", modelType);
        params.put("createdBy", createdBy);
        params.put("tenantId", this.tenantProvider.getTenantId());
        return this.sqlSessionTemplate.selectList("org.flowable.ui.modeler.domain.ModelHistory.selectModelHistoryByTypeAndCreatedBy", params);
    }

    public List<ModelHistory> findByModelId(String modelId) {
        return this.sqlSessionTemplate.selectList("org.flowable.ui.modeler.domain.ModelHistory.selectModelHistoryByModelId", modelId);
    }

    public void save(ModelHistory modelHistory) {
        if (modelHistory.getId() == null) {
            modelHistory.setId(this.idGenerator.generateId());
            modelHistory.setTenantId(this.tenantProvider.getTenantId());
            this.sqlSessionTemplate.insert("org.flowable.ui.modeler.domain.ModelHistory.insertModelHistory", modelHistory);
        } else {
            this.sqlSessionTemplate.update("org.flowable.ui.modeler.domain.ModelHistory.updateModelHistory", modelHistory);
        }

    }

    public void delete(ModelHistory modelHistory) {
        this.sqlSessionTemplate.delete("org.flowable.ui.modeler.domain.ModelHistory.deleteModelHistory", modelHistory);
    }
}

